package edu.pablocor.AndNote.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class BlocdeNotasDB {
	
	public static boolean insertarNota (Context context, String nombreNotas, String fechaNotas, String textoNotas){
		
		ContentValues registroInsNota = new ContentValues();
		
		registroInsNota.put( DbConstants.NOMBRENOTAS, nombreNotas);
		registroInsNota.put( DbConstants.FECHANOTAS, fechaNotas);
		registroInsNota.put( DbConstants.TEXTONOTAS, textoNotas);
		
		DatabaseOpenHelper dbhelper = new DatabaseOpenHelper(context, DbConstants.ANDNOTEDB, null, DbConstants.VERSION);
		SQLiteDatabase db = dbhelper.getWritableDatabase();

		return db.insert(DbConstants.TABLANOTAS, null, registroInsNota) != -1;
	}
	
	public static Cursor obtenerNotas (Context context){
		
		DatabaseOpenHelper dbhelper = new DatabaseOpenHelper(context, DbConstants.ANDNOTEDB, null, DbConstants.VERSION);
		SQLiteDatabase db = dbhelper.getReadableDatabase();
		
		return db.query(DbConstants.TABLANOTAS, null, null, null, null, null, null);
		
	}
	
	public static boolean eliminarNota (Context context, int idNotas){
		
		DatabaseOpenHelper dbhelper = new DatabaseOpenHelper(context, DbConstants.ANDNOTEDB, null, DbConstants.VERSION);
		SQLiteDatabase db = dbhelper.getWritableDatabase();

		//Log.d("ID", idNotas+"");
		return db.delete(DbConstants.TABLANOTAS, DbConstants.ID + " = ?", new String[]{idNotas+""}) != -1;
	}
	
	public static boolean actualizarNota (Context context, int idNotas, String textoNotas){
		
		ContentValues nuevoTexto = new ContentValues();
		
		nuevoTexto.put(DbConstants.TEXTONOTAS, textoNotas);
		
		DatabaseOpenHelper dbhelper = new DatabaseOpenHelper(context, DbConstants.ANDNOTEDB, null, DbConstants.VERSION);
		SQLiteDatabase db = dbhelper.getWritableDatabase();
		
		db.execSQL("DELETE FROM "+ DbConstants.TABLANOTAS + " WHERE " + DbConstants.ID + " = " + idNotas);
		
		return db.update(DbConstants.TABLANOTAS, nuevoTexto, DbConstants.IDNOTAS + " = ?", new String[]{idNotas+""}) != -1;
		
	}
}
